package com.hxd.tabactivityfragment.ui.fragment

import android.app.Activity
import android.content.Intent
import android.os.Environment
import com.hxd.tabactivityfragment.R
import com.hxd.tabactivityfragment.common.MyFragment
import com.hxd.tabactivityfragment.ui.activity.LoginActivity
import com.vondear.rxtool.RxSPTool
import kotlinx.android.synthetic.main.fragment_mine.*
import org.jetbrains.anko.support.v4.startActivity
import com.hxd.tabactivityfragment.mvp.presenter.MinePresenter
import com.hxd.tabactivityfragment.mvp.view.IMineView
import com.hxd.tabactivityfragment.ui.activity.WebActivity
import com.hxd.tabactivityfragment.util.CacheDataManager
import com.hxd.tabactivityfragment.util.L
import com.hxd.tabactivityfragment.util.StaticClassUtil
import java.io.File
import kotlin.collections.ArrayList


/**
 *  项目名：    Tab+Activity+Fragment
 *  包名：      com.hxd.tabactivityfragment.ui.fragment
 *  文件名:     HomeFragment
 *  创建者:     HXD
 *  创建时间:   2019/3/7 15:51
 *  描述:       我的界面
 */

class MineFragment: MyFragment(), IMineView {

    private var list:ArrayList<String> = ArrayList()
    private val fileUri = File(Environment.getExternalStorageDirectory().path + "/photo.jpg")

    private var mMinePresenter: MinePresenter? = null


    //设置单例静态方法
    companion object {
        fun newInstance():MineFragment{
            return MineFragment()
        }
    }

    override fun getTitleBarId(): Int {
        return 0
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_mine
    }

    override fun isStatusBarEnabled(): Boolean {
        return !super.isStatusBarEnabled()
    }

    override fun initData() {
        super.initData()
        list.add("拍照")
        list.add("打开相册")

        mMinePresenter = MinePresenter(this, getFragmentActivity()!!,context!!, token!!)
        mMinePresenter!!.initUserData(iv_photo_mine, tv_name_mine)
        mMinePresenter!!.bindView(this)

        sb_cache_mine.setRightText(CacheDataManager.getTotalCacheSize(context))
    }

    override fun initView() {
        //点击头像登录
        rl_login_mine.setOnClickListener {
            if(token!!.isEmpty()){
                startActivity<LoginActivity>()
            }else{
                mMinePresenter!!.showDialog(getFragmentActivity(), list)
            }
        }

        //隐私协议
        sb_agreement_mine.setOnClickListener {
            var intent = Intent(activity, WebActivity::class.java)
            intent.putExtra("url", "https://github.com/DemonHXD/FoodOrigins")
            startActivity(intent)
        }

        //关于我们
        sb_about_mine.setOnClickListener {
            var intent = Intent(activity, WebActivity::class.java)
            intent.putExtra("url", "https://github.com/DemonHXD/FoodOrigins")
            startActivity(intent)
        }

        //清除缓存
        sb_cache_mine.setOnClickListener {
            CacheDataManager.clearAllCache(context)
            // 重新获取应用缓存大小
            sb_cache_mine.setRightText(CacheDataManager.getTotalCacheSize(context))
        }

        //退出登录
        sb_exit_mine.setOnClickListener {
            startActivity<LoginActivity>()
            RxSPTool.remove(context, "photoPath")
            RxSPTool.remove(context, "token")
            tv_name_mine.text = "立即登录"
            iv_photo_mine.setImageResource(R.mipmap.mine_user)
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //调用相册
        if (requestCode == StaticClassUtil.ALBUM_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
            mMinePresenter!!.callAlbum(data, iv_photo_mine)
        }else if (requestCode == StaticClassUtil.CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK ){
            mMinePresenter!!.callCamera(iv_photo_mine)
        }
    }

    override fun getPhotoUri(): File {
        return fileUri
    }

    override fun showNetworkError() {

    }

    override fun getDataFail(msg: String) {
        myToast(msg)
    }

    override fun getPermissionSuccess() {
        mMinePresenter!!.takePhoto()
        L.i("这里在获取照片")
    }

    override fun getPermissionFail() {

    }


    override fun onDestroy() {
        super.onDestroy()
        mMinePresenter!!.destoryView()
    }
}